<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Form for ban IP.
 *
 *  History:
 *  2007-01-07  Gelin   Initially created.
 *  2007-01-27  Gelin   Modified for new headers process.
 *  2007-02-04  Gelin   Can prepare form by message_id.
 *  2007-07-22  Gelin   Bug #5 Error on adding empty ban rule.
 *                      - added check for headers number
 */

require_once('BaseForm.php');

/**
 *  Form for ban IP.
 */
class BanForm extends BaseForm {

    /**
     *  Creates array of form fields.
     *  @return array of form fields.
     */
    function prepareForm() {
        global $config;
        global $banChecker;
        $form['action'] = '?action=admin-ban-add-submit';
        //prepare common options list
        $options = array();
        $options[] = array('value' => '', 'label' => '');   //first option is empty
        foreach ($config['check_header'] as $key => $value) {
            $options[] = array('value' => $key, 'label' => $value['label']);
        }
        //make fields
        $i = 0;
        $headers = getParameter('header');
        $values = getParameter('value');
        foreach ($config['check_header'] as $key => $value) {
            $form['header'][$i] = $this->prepareFormField("header[$i]", $headers[$i]);
            $form['header'][$i]['options'] = $options;
            $form['value'][$i] = $this->prepareFormField("value[$i]", $values[$i]);
            $i++;
        }
        //fill by message_id
        $messageID = getParameter('message_id');
        if (isset($messageID)) {
            $headers = $banChecker->getMessageHeaders($messageID);
            $i = 0;
            foreach ($headers as $key => $header) {
                $form['header'][$i]['value'] = $key;
                $form['value'][$i]['value'] = $header['value'];
                $i++;
            }
        }
        return $form;
    }

    /**
     *  Extracts form fields from request.
     *  @return ban form fields.
     */
    function getFromRequest() {
        $headers = $this->getFormField('header');
        $values = $this->getFormField('value');
        $result = array();
        $result['id'] = $this->getFormField('id');
        if (count($headers) > 0) {
            foreach ($headers as $index => $header) {
                $value = $values[$index];
                if ($header != '' && $value != '') {
                    $result['headers'][$header]['value'] = $value;
                }
            }
        }
        $result['create_time'] = time();
        return $result;
    }

    /**
     *  Prepares ban form to display.
     *  @param  $ban array of ban fields to prepare
     *  @return modified array of ban fields
     */
    function prepareToDisplay($ban) {
        global $config;
        global $banChecker;
        $ban['create_time'] = date($config['format']['time'], $ban['create_time']);
        $ban['headers'] = $banChecker->formatHeaders($ban['headers']);
        //$ban['host'] = @gethostbyaddr($ban['ip']);
        return $ban;
    }


}

?>